 require 'htree'
 require 'rexml/document'
 require 'open-uri'
#http://redhanded.hobix.com/inspect/noXpathOnMessyHtmlIsJustAsEasyInRuby.html
class WebWrapper < ActiveRecord::Base

 def read_xhtml_from(url)
   if defined?HTTP_PROXY
     proxy = "http://#{HTTP_PROXY}:#{HTTP_PROXY_PORT}"
   else
     proxy = false
   end
   puts "WebWrapper::opening #{url}" if defined?DEBUG
   open(url, :proxy => proxy, "User-Agent" => "Mozilla/5.0 (X11; U; Linux i686; fr-FR; rv:1.9.1) Gecko/20090624 Ubuntu/9.04 (jaunty) Firefox/3.5" ) do |f|
     f_string =  f.read.untaint
     f_string = Iconv.conv('UTF-8',f.charset, f_string)
     HTree.parse f_string
   end.each_child do |child|
     if child.respond_to? :qualified_name
       puts "This child respond to a :qualified_name(#{child.qualified_name})..." if defined?DEBUG
       doc = ""
       child.display_xml(doc)
       if child.qualified_name == 'html'
         puts '...and was \'html\'!' if defined?DEBUG
         return child.to_rexml
       else
         puts "...but wasn't 'html'(#{child.qualified_name})!" if defined?DEBUG
       end
     else puts 'This child doesn\'t respond to a :qualified_name !' if defined?DEBUG
     end
   end
 end

  def wrap_source_url(url)
    ret = ''
    if html = read_xhtml_from(url)
      html.each_element(xpath) do |e|
        ret = e.attribute('src').to_s
        break
      end
      return ret
    end
  end

  def self.wrap_url(link)
    if wrappers = self.find(:all)
      wrappers.each do |wrapper|
        if link =~ Regexp.new(wrapper.url_regex)
          return wrapper.wrap_source_url(link)
        end
      end
    end
    return nil
  end

end
